/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package viewControl;

import entity.*;
import java.util.*;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Jeppe Andersen
 */
public class TrinmaalPanel extends javax.swing.JPanel {

    private Session session;
    private Laerer currentLaerer;

    /**
     * Creates new form SecretaryPanel
     */
    public TrinmaalPanel(Session session) {
        this.session = session;
        this.currentLaerer = null;
        initComponents();
    }

    public TrinmaalPanel(Session session, Laerer laerer) {
        this.session = session;
        this.currentLaerer = laerer;
        initComponents();
    }

    protected void update() {
        listTrinmaal.removeAll();
        listTrinmaal.validate();
        String hql;
        cbFag.removeAllItems();
        cbFag.addItem("Fag");
        if (currentLaerer == null) {
            hql = "from Fag";
            try {
                session.beginTransaction();
                Query q = session.createQuery(hql);
                Object[] fList = q.list().toArray();
                Arrays.sort(fList);
                for (int i = 0; i < fList.length; i++) {
                    cbFag.addItem(fList[i]);
                }
                session.getTransaction().commit();
                validate();
            } catch (HibernateException he) {
                System.out.println(he.getLocalizedMessage());
            }

            // Udfyld elev combobox.
            cbElevNavn.removeAllItems();
            cbElevNavn.addItem("Elev");
            hql = "from Elev where udskrevet=null";
            try {
                session.beginTransaction();
                Query q = session.createQuery(hql);
                Object[] eList = q.list().toArray();
                Arrays.sort(eList);
                for (int i = 0; i < eList.length; i++) {
                    cbElevNavn.addItem(eList[i]);
                }
                session.getTransaction().commit();
                session.flush();
            } catch (HibernateException he) {
                System.out.println(he.getLocalizedMessage());
            }
        } else if (currentLaerer != null) {
            ArrayList holdfags = new ArrayList();
            ArrayList elever = new ArrayList();
            ArrayList finallist = new ArrayList();

            holdfags.addAll(currentLaerer.getHoldfags());

            // Udfyld elev combobox.
            cbElevNavn.removeAllItems();
            cbElevNavn.addItem("Elev");
            for (int i = 0; i < holdfags.size(); i++) {
                Holdfag hf = (Holdfag) holdfags.get(i);
                elever.addAll(hf.getHold().getElevs());
                cbFag.addItem(hf.getFag());
                for (int j = 0; j < elever.size(); j++) {
                    Elev e = (Elev) elever.get(j);
                    if (!finallist.contains(e)) {
                        finallist.add(e);
                        cbElevNavn.addItem(e);
                    }
                }
            }
        }
    }

    protected void updatePopUp() {
        String hql;
        cbFag1.removeAllItems();
        cbFag1.addItem("Fag");
        hql = "from Fag";
        try {
            session.beginTransaction();
            Query q = session.createQuery(hql);
            ArrayList<Fag> resultList = (ArrayList<Fag>) q.list();
            for (int i = 0; i < resultList.size(); i++) {
                cbFag1.addItem(resultList.get(i));
            }
            session.getTransaction().commit();
            validate();
        } catch (HibernateException he) {
            System.out.println(he.getLocalizedMessage());
        }

    }

    public void updateCat() {
        cbCat.removeAllItems();
        String hql;
        cbCat.addItem("Kategori");
        Fag to = (Fag) cbFag.getSelectedItem();
        hql = "from Trinoverskrift where fag = '" + to.getId() + "'";
        try {
            session.beginTransaction();
            Query q = session.createQuery(hql);
            session.getTransaction().commit();
            ArrayList finalList = new ArrayList();
            ArrayList<Trinoverskrift> resultList = (ArrayList<Trinoverskrift>) q.list();
            for (int i = 0; i < resultList.size(); i++) {
                ArrayList<Trinbeskrivelse> tbList = new ArrayList();
                tbList.addAll(resultList.get(i).getTrinbeskrivelses());
                for (int j = 0; j < tbList.size(); j++) {
                    if (tbList.get(j).getNiveau().intValue() == ((Integer) cbNiveau.getSelectedItem()).intValue()) {
                        if (!finalList.contains(resultList.get(i))) {
                            finalList.add(resultList.get(i));
                        }
                    }
                }
            }
            for (int i = 0; i < finalList.size(); i++) {
                cbCat.addItem(finalList.get(i));
            }
            validate();
        } catch (HibernateException he) {
            System.out.println(he.getLocalizedMessage());
        }
    }

    public void updateCatPopup() {
        cbCat1.removeAllItems();
        String hql;
        cbCat1.addItem("Kategori");
        Fag to = (Fag) cbFag1.getSelectedItem();
        hql = "from Trinoverskrift where fag = '" + to.getId() + "'";
        try {
            session.beginTransaction();
            Query q = session.createQuery(hql);
            ArrayList<Trinoverskrift> resultList = (ArrayList<Trinoverskrift>) q.list();
            for (int i = 0; i < resultList.size(); i++) {
                cbCat1.addItem(resultList.get(i));
            }

            session.getTransaction().commit();
            validate();
        } catch (HibernateException he) {
            System.out.println(he.getLocalizedMessage());
        }
    }

    public void updateTrinmaal() {
        String hql;
        listTrinmaal.setListData(new Object[0]);
        Trinoverskrift to = (Trinoverskrift) cbCat.getSelectedItem();
        hql = "from Trinbeskrivelse where overskriftid='" + to.getId() + "' and niveau='" + (Integer) cbNiveau.getSelectedItem() + "'";
        try {
            session.beginTransaction();
            Query q = session.createQuery(hql);
            ArrayList<Trinbeskrivelse> resultList = (ArrayList<Trinbeskrivelse>) q.list();
            listTrinmaal.setListData(resultList.toArray());
            session.getTransaction().commit();
            session.flush();
        } catch (HibernateException he) {
            System.out.println(he.getLocalizedMessage());
        }
    }

    public void updateNiveau() {
        String hql;
        ArrayList<Trinoverskrift> trinOverskrift = new ArrayList();
        ArrayList trinBeskrivelser = new ArrayList();
        ArrayList finallist = new ArrayList();
        ArrayList niveauer = new ArrayList();


        //Udfyld niveau combobox.
        cbNiveau.removeAllItems();
        cbNiveau.addItem("Niveau");

        Fag sFag = (Fag) cbFag.getSelectedItem();
        trinOverskrift.addAll(sFag.getTrinoverskrifts());
        for (int i = 0; i < trinOverskrift.size(); i++) {
            trinBeskrivelser.addAll(trinOverskrift.get(i).getTrinbeskrivelses());
        }

        for (int i = 0; i < trinBeskrivelser.size(); i++) {
            Trinbeskrivelse tb = (Trinbeskrivelse) trinBeskrivelser.get(i);
            if (!finallist.contains(tb.getNiveau())) {
                finallist.add(tb.getNiveau());
                niveauer.add(tb.getNiveau());

            }
        }

        Collections.sort(niveauer);
        for (int i = 0; i < niveauer.size(); i++) {
            cbNiveau.addItem(niveauer.get(i));
        }
    }

    public void createTrinmaal() {
        Trinbeskrivelse tm = new Trinbeskrivelse();
        tm.setBeskrivelse(txtaBeskrivelse.getText());
        tm.setTrinoverskrift((Trinoverskrift) cbCat1.getSelectedItem());
        tm.setNiveau(cbNiveau1.getSelectedIndex());
        session.beginTransaction();
        session.save(tm);
        session.getTransaction().commit();
        session.beginTransaction();
        Fag f = (Fag) cbFag1.getSelectedItem();
        session.update(f);
        session.getTransaction().commit();
        txtaBeskrivelse.setText("");
        JOptionPane.showMessageDialog(null, "Trinmål oprettet", "Info", 1);
        btnOpret.setEnabled(false);
    }

    public void addTrinmaalToStudent() {
        Trinbeskrivelse tb = (Trinbeskrivelse) listTrinmaal.getSelectedValue();
        Elev el = (Elev) cbElevNavn.getSelectedItem();
        try {
            session.flush();
            session.beginTransaction();
            PotentialeId pid = new PotentialeId(el.getCpr(), tb.getId());
            Potentiale p = new Potentiale(pid, el, tb);
            p.setKompetence(false);
            p.setPrioritet(5);
            session.save(p);
            session.getTransaction().commit();
            session.beginTransaction();           
            session.update(el);
            session.getTransaction().commit();
            JOptionPane.showMessageDialog(null, "Trinmålet er blevet tilføjet til " + el.getFornavn() + " " + el.getEfternavn() + ".", "Info", 1);
        } catch (HibernateException he) {
            JOptionPane.showMessageDialog(null, "Kunne ikke tilføjes. Da eleven allerede har det tilknyttet", "Info", 1);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popUpMenu = new javax.swing.JPopupMenu();
        miRet = new javax.swing.JMenuItem();
        miSlet = new javax.swing.JMenuItem();
        createDialog = new javax.swing.JDialog();
        cbCat1 = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        cbNiveau1 = new javax.swing.JComboBox();
        cbFag1 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        txtKategori = new javax.swing.JTextField();
        btnTilføjKategori = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtaBeskrivelse = new javax.swing.JTextArea();
        btnOpret = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        listTrinmaal = new javax.swing.JList();
        cbElevNavn = new javax.swing.JComboBox();
        btnAddTrinToStudent = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        cbNiveau = new javax.swing.JComboBox();
        cbFag = new javax.swing.JComboBox();
        btnCreatePopup = new javax.swing.JButton();
        cbCat = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        popUpMenu.setEnabled(false);

        miRet.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/edit.png"))); // NOI18N
        miRet.setText("Ret");
        miRet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miRetActionPerformed(evt);
            }
        });
        popUpMenu.add(miRet);

        miSlet.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/delete.png"))); // NOI18N
        miSlet.setText("Slet");
        miSlet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miSletActionPerformed(evt);
            }
        });
        popUpMenu.add(miSlet);

        createDialog.getContentPane().setLayout(null);

        cbCat1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kategori" }));
        cbCat1.setEnabled(false);
        cbCat1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbCat1ActionPerformed(evt);
            }
        });
        createDialog.getContentPane().add(cbCat1);
        cbCat1.setBounds(10, 140, 220, 20);

        jLabel5.setText("Trinmål efter:");
        createDialog.getContentPane().add(jLabel5);
        jLabel5.setBounds(10, 70, 70, 14);

        cbNiveau1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Klassetrin", "1 Klasse", "2 Klasse", "3 Klasse", "4 Klasse", "5 Klasse", "6 Klasse", "7 Klasse", "8 Klasse", "9 Klasse", "10 Klasse" }));
        cbNiveau1.setToolTipText("");
        cbNiveau1.setEnabled(false);
        cbNiveau1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbNiveau1ActionPerformed(evt);
            }
        });
        createDialog.getContentPane().add(cbNiveau1);
        cbNiveau1.setBounds(10, 90, 220, 20);

        cbFag1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Fag" }));
        cbFag1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbFag1ActionPerformed(evt);
            }
        });
        createDialog.getContentPane().add(cbFag1);
        cbFag1.setBounds(10, 30, 220, 20);

        jLabel6.setText("Kategori");
        createDialog.getContentPane().add(jLabel6);
        jLabel6.setBounds(270, 10, 50, 14);
        createDialog.getContentPane().add(txtKategori);
        txtKategori.setBounds(270, 30, 390, 20);

        btnTilføjKategori.setText("Tilføj ny kategori");
        btnTilføjKategori.setEnabled(false);
        btnTilføjKategori.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTilføjKategoriActionPerformed(evt);
            }
        });
        createDialog.getContentPane().add(btnTilføjKategori);
        btnTilføjKategori.setBounds(270, 60, 390, 23);

        jLabel7.setText("Beskrivelse");
        createDialog.getContentPane().add(jLabel7);
        jLabel7.setBounds(270, 110, 53, 14);

        txtaBeskrivelse.setColumns(20);
        txtaBeskrivelse.setRows(5);
        txtaBeskrivelse.setEnabled(false);
        txtaBeskrivelse.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtaBeskrivelseKeyReleased(evt);
            }
        });
        jScrollPane3.setViewportView(txtaBeskrivelse);

        createDialog.getContentPane().add(jScrollPane3);
        jScrollPane3.setBounds(270, 130, 390, 120);

        btnOpret.setText("Opret nyt trinmål");
        btnOpret.setEnabled(false);
        btnOpret.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpretActionPerformed(evt);
            }
        });
        createDialog.getContentPane().add(btnOpret);
        btnOpret.setBounds(270, 260, 390, 23);

        setLayout(null);

        listTrinmaal.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        listTrinmaal.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listTrinmaal.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        listTrinmaal.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                listTrinmaalMouseReleased(evt);
            }
        });
        listTrinmaal.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                listTrinmaalPropertyChange(evt);
            }
        });
        jScrollPane2.setViewportView(listTrinmaal);

        add(jScrollPane2);
        jScrollPane2.setBounds(10, 180, 970, 410);

        cbElevNavn.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Elev" }));
        cbElevNavn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbElevNavnActionPerformed(evt);
            }
        });
        add(cbElevNavn);
        cbElevNavn.setBounds(310, 40, 260, 20);

        btnAddTrinToStudent.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnAddTrinToStudent.setText("Tilføj Trinmål Til Elev");
        btnAddTrinToStudent.setEnabled(false);
        btnAddTrinToStudent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddTrinToStudentActionPerformed(evt);
            }
        });
        add(btnAddTrinToStudent);
        btnAddTrinToStudent.setBounds(310, 90, 260, 50);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Vælg Elev Som Du Vil Tilføje Trinmål Til Elevens Potenttialer:");
        add(jLabel4);
        jLabel4.setBounds(310, 10, 340, 14);

        cbNiveau.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Klassetrin", "1 Klasse", "2 Klasse", "3 Klasse", "4 Klasse", "5 Klasse", "6 Klasse", "7 Klasse", "8 Klasse", "9 Klasse", "10 Klasse" }));
        cbNiveau.setToolTipText("");
        cbNiveau.setEnabled(false);
        cbNiveau.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbNiveauActionPerformed(evt);
            }
        });
        add(cbNiveau);
        cbNiveau.setBounds(10, 90, 260, 20);

        cbFag.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Fag" }));
        cbFag.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbFagActionPerformed(evt);
            }
        });
        add(cbFag);
        cbFag.setBounds(10, 40, 260, 20);

        btnCreatePopup.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnCreatePopup.setText("Opret Nyt Trinmål Her");
        btnCreatePopup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreatePopupActionPerformed(evt);
            }
        });
        add(btnCreatePopup);
        btnCreatePopup.setBounds(10, 600, 200, 30);

        cbCat.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kategori" }));
        cbCat.setEnabled(false);
        cbCat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbCatActionPerformed(evt);
            }
        });
        add(cbCat);
        cbCat.setBounds(10, 120, 260, 20);

        jLabel1.setText("Trinmål efter:");
        add(jLabel1);
        jLabel1.setBounds(10, 70, 70, 14);

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);
        add(jSeparator2);
        jSeparator2.setBounds(290, 0, 10, 180);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Find Trinmål:");
        add(jLabel2);
        jLabel2.setBounds(10, 10, 72, 14);

        jLabel3.setText("Tip : Højerklik på listen for at rette eller slette trinmål.");
        add(jLabel3);
        jLabel3.setBounds(230, 600, 270, 30);
    }// </editor-fold>//GEN-END:initComponents

    private void cbNiveauActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbNiveauActionPerformed
        if (cbNiveau.getSelectedIndex() > 0) {
            updateCat();
            cbCat.setEnabled(true);
        } else {
            listTrinmaal.setListData(new Object[0]);
            cbCat.setEnabled(false);
            cbCat.setSelectedIndex(0);
        }

    }//GEN-LAST:event_cbNiveauActionPerformed

    private void cbFagActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbFagActionPerformed
        if (cbFag.getSelectedIndex() > 0) {
            updateNiveau();
            cbNiveau.setEnabled(true);
        } else {
            listTrinmaal.setListData(new Object[0]);
            cbNiveau.setEnabled(false);
            cbCat.setEnabled(false);
        }
    }//GEN-LAST:event_cbFagActionPerformed

    private void miRetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miRetActionPerformed
        Trinbeskrivelse tm = (Trinbeskrivelse) listTrinmaal.getSelectedValue();
        try {
            String reply = null;
            try {
                reply = JOptionPane.showInputDialog(null, "Skriv ny trinmåls beskrivelse i feltet under", "Ret trinmål", 3, null, null, tm.getBeskrivelse()).toString();
            } catch (NullPointerException e) {
                // Do nothing.
            }
            if (reply != null) {
                tm.setBeskrivelse(reply);
                session.beginTransaction();
                session.update(tm);
                session.getTransaction().commit();
                session.flush();
                updateTrinmaal();
                JOptionPane.showMessageDialog(null, "Trinmåls beskrivelsen er rettet", "Info", 1);
            }
        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        }
    }//GEN-LAST:event_miRetActionPerformed

    private void miSletActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miSletActionPerformed
        try {
            int reply = JOptionPane.showConfirmDialog(null, "Er du sikker på du vil slette det valgte trinmål?", "Slet trinmål", 2);
            if (reply == JOptionPane.YES_OPTION) {
                session.beginTransaction();
                session.delete(listTrinmaal.getSelectedValue());
                session.getTransaction().commit();
                session.flush();
                updateTrinmaal();
                JOptionPane.showMessageDialog(null, "Trinmålet er slettet", "Info", 1);
            }
        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        }
    }//GEN-LAST:event_miSletActionPerformed

    private void listTrinmaalMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listTrinmaalMouseReleased
        if (!listTrinmaal.isSelectionEmpty()) {
            listTrinmaal.setComponentPopupMenu(popUpMenu);
            if (cbElevNavn.getSelectedIndex() > 0) {
                btnAddTrinToStudent.setEnabled(true);
            }
        } else {
            listTrinmaal.setComponentPopupMenu(null);
            btnAddTrinToStudent.setEnabled(false);
        }
    }//GEN-LAST:event_listTrinmaalMouseReleased

    private void cbCatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbCatActionPerformed
        if (cbFag.getSelectedIndex() > 0 && cbNiveau.getSelectedIndex() > 0 && cbCat.getSelectedIndex() > 0) {
            updateTrinmaal();
        } else {
            listTrinmaal.setListData(new Object[0]);
            btnAddTrinToStudent.setEnabled(false);
        }
    }//GEN-LAST:event_cbCatActionPerformed

    private void listTrinmaalPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_listTrinmaalPropertyChange
        if (listTrinmaal.isSelectionEmpty()) {
            btnAddTrinToStudent.setEnabled(false);
        }
    }//GEN-LAST:event_listTrinmaalPropertyChange

    private void btnAddTrinToStudentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddTrinToStudentActionPerformed
        addTrinmaalToStudent();
    }//GEN-LAST:event_btnAddTrinToStudentActionPerformed

    private void cbElevNavnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbElevNavnActionPerformed
        if (!listTrinmaal.isSelectionEmpty()) {
            btnAddTrinToStudent.setEnabled(true);
        }
    }//GEN-LAST:event_cbElevNavnActionPerformed

    private void cbCat1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbCat1ActionPerformed
        // TODO add your handling code here:
        if (cbFag1.getSelectedIndex() > 0 && cbNiveau1.getSelectedIndex() > 0 && cbCat1.getSelectedIndex() > 0) {
            txtaBeskrivelse.setEnabled(true);
        } else {
            txtaBeskrivelse.setText("");
            txtaBeskrivelse.setEnabled(false);
            btnOpret.setEnabled(false);
        }
    }//GEN-LAST:event_cbCat1ActionPerformed

    private void cbNiveau1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbNiveau1ActionPerformed
        if (cbNiveau1.getSelectedIndex() > 0) {
            updateCatPopup();
            cbCat1.setEnabled(true);
            btnTilføjKategori.setEnabled(true);
            txtKategori.setEnabled(true);
        } else {
            cbCat1.setEnabled(false);
            cbCat1.setSelectedIndex(0);
            txtKategori.setEnabled(false);
            btnTilføjKategori.setEnabled(false);
        }
    }//GEN-LAST:event_cbNiveau1ActionPerformed

    private void cbFag1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbFag1ActionPerformed
        if (cbFag1.getSelectedIndex() > 0) {
            cbNiveau1.setEnabled(true);
        } else {
            cbNiveau1.setEnabled(false);
            cbCat1.setEnabled(false);
        }
    }//GEN-LAST:event_cbFag1ActionPerformed

    private void btnTilføjKategoriActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTilføjKategoriActionPerformed
        // TODO add your handling code here:
        String text = txtKategori.getText();
        if (text.length() > 0) {
            Trinoverskrift to = new Trinoverskrift();
            Fag f = (Fag) cbFag1.getSelectedItem();
            to.setFag(f);
            to.setOverskrift(text);
            session.flush();
            session.beginTransaction();
            session.save(to);
            session.getTransaction().commit();
            JOptionPane.showMessageDialog(null, "Trinmål kategori oprettet", "Info", 1);
            updateCatPopup();
            txtKategori.setText(null);
        }
    }//GEN-LAST:event_btnTilføjKategoriActionPerformed

    private void txtaBeskrivelseKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtaBeskrivelseKeyReleased
        if (txtaBeskrivelse.getText().length() != 0) {
            btnOpret.setEnabled(true);
        } else {
            btnOpret.setEnabled(false);
        }
    }//GEN-LAST:event_txtaBeskrivelseKeyReleased

    private void btnOpretActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpretActionPerformed
        createTrinmaal();
        update();
    }//GEN-LAST:event_btnOpretActionPerformed

    private void btnCreatePopupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreatePopupActionPerformed
        updatePopUp();
        createDialog.setSize(750, 350);
        createDialog.setLocationRelativeTo(null);
        createDialog.setVisible(true);
    }//GEN-LAST:event_btnCreatePopupActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddTrinToStudent;
    private javax.swing.JButton btnCreatePopup;
    private javax.swing.JButton btnOpret;
    private javax.swing.JButton btnTilføjKategori;
    private javax.swing.JComboBox cbCat;
    private javax.swing.JComboBox cbCat1;
    private javax.swing.JComboBox cbElevNavn;
    private javax.swing.JComboBox cbFag;
    private javax.swing.JComboBox cbFag1;
    private javax.swing.JComboBox cbNiveau;
    private javax.swing.JComboBox cbNiveau1;
    private javax.swing.JDialog createDialog;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JList listTrinmaal;
    private javax.swing.JMenuItem miRet;
    private javax.swing.JMenuItem miSlet;
    private javax.swing.JPopupMenu popUpMenu;
    private javax.swing.JTextField txtKategori;
    private javax.swing.JTextArea txtaBeskrivelse;
    // End of variables declaration//GEN-END:variables
}
